<template>
  <div ref="chartRef" style="width: 100%; height: 180px"/>
</template>

<script setup>
import {onMounted, watch, nextTick, ref, defineEmits} from "vue";
import {useEcharts} from "../../../../hook/ useEcharts";
import * as echarts from "echarts";

const {chartRef, initChart, updateChart} = useEcharts();
const props = defineProps({
  value: {
    type: Object,
    default: () => ({tgRate: [], fgRate: [], xData: []}),
  },
});

const emits = defineEmits(['click']);

const option = {
  tooltip: {
    trigger: 'item',
    axisPointer: { // 坐标轴指示器，坐标轴触发有效
      type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
    }
  },
  grid: {
    left: '2%',
    right: '1%',
    bottom: '12%',
    top: '20%',
    containLabel: true
  },
  legend: {
    data: ['上线通过率', '系统字段中文覆盖率'],
    top: '3%',
    textStyle: {
      color: "#fff"
    },
    itemWidth: 15,
    itemHeight: 10,
  },
  dataZoom: [
    {
      type: 'slider',
      show: true,
      height: '10px',
      bottom: '10px',
      left: '10%',
      right: '10%',
      zoomLoxk: true,
      showDetail: false,
      borderColor: '#8FDFFE',
      backgroundColor: '#8fdffe99',
      handleColor: '#8FDFFE',
      handleSize: 20,
      handleStyle: {
        borderColor: '#8FDFFE',
        shadowBlur: 4,
        shadowOffsetX: 1,
        shadowOffsetY: 1,
        shadowColor: '#e5e5e5'
      },
      start: 0,
      end: 10
    }
  ],
  xAxis: {
    type: 'category',
    data: props.value.xData,
    axisLine: {
      lineStyle: {
        color: '#0444AD'
      }
    },
    axisLabel: {
      textStyle: {
        color: "#fff"
      }
    },
    axisTick: {
      show: false,
    },
  },
  yAxis: [{
    type: 'value',
    name: "单位：%",
    nameTextStyle: {
      color: "#fff"
    },
    axisLine: {
      show: false,
      lineStyle: {
        color: '#cdd5e2'
      }
    },
    splitLine: {
      show: false,
    },
    axisLabel: {
      textStyle: {
        color: "#fff"
      }
    },
  },
    {
      type: "value",
      axisLine: {
        lineStyle: {
          color: '#cdd5e2'
        }
      },
      splitLine: {
        show: false,
      },
      axisLabel: {
        show: true,
        formatter: "{value} 万条", //右侧Y轴文字显示
        textStyle: {
          color: "#fff"
        }
      }
    }
  ],
  series: [{
    name: '上线通过率',
    type: 'bar',
    barWidth: '15px',
    itemStyle: {
      normal: {
        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
          offset: 0,
          color: '#3C7DFB'
        }, {
          offset: 1,
          color: '#0A285F'
        }]),
        barBorderRadius: 8,
      },
    },
    data:props.value.tgRate,
  },
    {
      name: '系统字段中文覆盖率',
      type: 'bar',
      barWidth: '15px',
      itemStyle: {
        normal: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
            offset: 0,
            color: '#3CE2FB'
          }, {
            offset: 1,
            color: '#0A585F4D'
          }]),
          barBorderRadius: 8,
        }

      },
      data: props.value.fgRate,
    },
  ],
};

watch(() => {
  return props.value
}, (newVal) => {
  updateChart({
    xAxis: {
      data: newVal.xData,
    },
    series: [{
      name: '上线通过率',
      data: newVal.tgRate,
    }, {
      name: '系统字段中文覆盖率',
      data: newVal.fgRate,
    }],

  })
}, {deep: true})


onMounted(() => {
  // 初始化图表
  initChart(option);
  nextTick(() => {
    const chart = echarts.init(chartRef.value)
    chart.on('click', (params) => {
      emits('click', params)
    })
  })
});

</script>

<style scoped lang="scss">

</style>
